/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package things;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;

/**
 *
 * @author Ben
 */
public class Draw {

    public int width;
    public int height;
    public World w;

    public Draw(World w, Color c) {
        this.w = w;
        this.width = w.width;
        this.height = w.height;
        drawHorizontalLines(c);
    }

    /**
     * Draws 3 horizontal and 3 vertical lines seperating the board into 16 components.
     * @param g2 Graphics object to draw on
     * @param c Color to make the lines
     */
    public void drawHorizontalLines(Color c) {
        Graphics2D g2 = w.g2;
        g2.setColor(c);
        // Horizontal lines
        g2.drawLine(0, (int) (height * 0.25), width, (int) (height * 0.25));
        g2.drawLine(0, (int) (height * 0.5), width, (int) (height * 0.5));
        g2.drawLine(0, (int) (height * 0.75), width, (int) (height * 0.75));
        // Vertical lines
        g2.drawLine((int) (width * 0.25), 0, (int) (width * 0.25), height);
        g2.drawLine((int) (width * 0.5), 0, (int) (width * 0.5), height);
        g2.drawLine((int) (width * 0.75), 0, (int) (width * 0.75), height);
    }

    /**
     * Fills in the background
     * @param g2 Graphics object to draw on
     * @param c Color to make the background
     */
    public void drawBackground(Color c) {
        Graphics2D g2 = w.g2;
        g2.setColor(c);
        g2.fillRect(0, 0, width, height);
    }

    /**
     * Draws a pit at the cell x,y. Top left is [0,0]
     * @param x X parameter of the pit
     * @param y Y parameter of the pit
     * @param g2 Graphics object to draw on
     * @param c Color to make the pit
     */
    public void drawPit(int x, int y, Color c) {
        Graphics2D g2 = w.g2;
        if (x > 3 || x < 0 || y > 3 || y < 0) {
            throw new IndexOutOfBoundsException("Please use a number between 0 and 3");
        } else {
            Dimension cellSize = new Dimension((int) (width * 0.25), (int) (height * 0.25));
            Dimension cellLocation = new Dimension(cellSize.width * x, cellSize.height * y);
            g2.setColor(c);
            int topLeftX = cellLocation.width + 1;
            int topLeftY = cellLocation.height + 1;
            int cellWidth = cellSize.width - 1;
            int cellHeight = cellSize.height - 1;
            if (x == 0 && y == 0) { // If it's the top left cell, shift up and left 1 pixel, stretch right and down 1 pixel
                topLeftX -= 1;
                topLeftY -= 1;
                cellWidth += 1;
                cellHeight += 1;
            }
            if (x == 0 && y != 0) { // If it's on the left, but not top, move left one pixel and wider one pixel
                topLeftX -= 1;
                cellWidth += 1;
            }
            if (y == 0) { // If it's on the top row, move up one pixel and make one pixel taller
                topLeftY -= 1;
                cellHeight += 1;
            }

            g2.fillRect(topLeftX, topLeftY, cellWidth, cellHeight);
        }
    }
}
